// Trade-Policy Dynamics: Evidence from 60 Years of U.S.-China Trade
// Alessandria,  Khan, Khederlarian, Ruhl, and Steinberg

// inputs:	dataset_sitc.dta
//			exporter_moments.dta


// outputs:	inputs_calibration_gt.dta
//			inputs_calibration_g.dta
//			inputs_calibration_sectors.dta

// This file creates the moments needed to calibrate the structural model. One additional
// file, "inputs_calibration_gapelast.dta" is created in the file "10 trade analysis.do".


clear all 
set varabbrev off

timer on 1
global dir_raw "../01 Raw data/"
global dir_int "../20 Intermediate files/"
global dir_con "../02 Raw data confidential/Intermediate files/"

use "${dir_int}dataset_sitc.dta", clear


drop if nntr_med==.
drop if gap_med==.
drop if phase!=0 | fta==1 | ntr!=1
egen temptag = tag(sitc year)
drop if temptag==0
// I changed this. Is it okay? tk
//keep sitc* sector* year nntr_med ntr_med gap_med applied_* delast_S18_usachn delast_S18_usa delast_S18_all
  keep sitc* sector* year nntr_med ntr_med* gap_med applied_* delast_S18_usachn delast_S18_usa delast_S18_all

g mfn_tariff=applied_ntr 
replace mfn_tariff=nntr_med if nntr_med<mfn_tariff & nntr_med!=. & mfn_tariff!=.

////////////////////////////////////////////////////////////////////////////////
// inputs_calibration_gt.dta
{
preserve
	sort sitc year
	keep sitc year mfn_tariff applied_ntr
	label var applied_ntr "No replacement (NNTR>=<MFN)"
	label var mfn_tariff "With replacement (NNTR>=MFN)"
	compress
	save "${dir_int}inputs_calibration_gt.dta", replace
restore
}

////////////////////////////////////////////////////////////////////////////////
// inputs_calibration_g.dta
{
egen temptag = tag(sitc)
drop if temptag==0
preserve
	sort sitc
	keep sitc nntr_med gap_med sector*
	save "${dir_int}inputs_calibration_g.dta", replace
restore
}

////////////////////////////////////////////////////////////////////////////////
// inputs_calibration_sectors.dta
{
	foreach x in delast_S18_usachn delast_S18_usa delast_S18_all {
		egen `x'_agg=mean(`x')
	}

	merge m:1 sector_n using "${dir_con}exporter_moments.dta"
	tab sector if _merge!=3

	keep sitc sector* delast_S18_* *_0507 *_0407 *_0406 *_agg_???? gap_med

	foreach y in 0507 0407 0406 {
		foreach x in export_partic export_intens entry_rate exit_rate incumbent_premium cov_lexports cov_lexpxl {
			egen temp = mean(`x'_agg_`y')
			replace `x'_agg_`y'=temp if sector_n==15
			drop temp
		}
	}

	egen temptag=tag(sector) if sector_n!=15
	foreach y in 0507 0407 0406 {
		foreach x in export_partic export_intens entry_rate exit_rate incumbent_premium cov_lexports cov_lexpxl numfirms {
			egen temp = mean(`x'_`y'/(temptag==1))
			replace `x'_`y'=temp if sector_n==15
			drop temp
		}
	}

	foreach x in delast_S18_usachn delast_S18_usa delast_S18_all gap_med {
		bysort sector: egen temp=mean(`x')
		replace `x'=temp
		drop temp
	}
	drop temptag sitc
	egen temptag=tag(sector)
	drop if temptag==0
	drop temptag

	set obs `=_N+1'
	replace sector="Aggregate" if _n==`=_N'
	foreach y in 0507 0407 0406 {
		foreach x in export_partic export_intens entry_rate exit_rate incumbent_premium cov_lexports cov_lexpxl {
			replace `x'_`y'=`x'_agg_`y'[_n-1] if _n==`=_N'
		}
	}
	foreach x in delast_S18_usachn delast_S18_usa delast_S18_all {
		replace `x'=`x'_agg[_n-1] if _n==`=_N'
	}

	drop *_agg *_agg_????

	label var export_partic_0407 		"Exporters over Number Firms, by sector, 2004-07"
	label var export_intens_0407 		"Exports over Sales (exporters only), mean by sector, 2004-07"
	label var entry_rate_0407 			"New Exporters over Total Exporters, by sector, 2004-07"
	label var exit_rate_0407 			"Stopping Exporters over Total Exporters, by sector, 2004-07"
	label var incumbent_premium_0407	"Exports Incumbents over Exports New Exporters, mean by sector, 2004-07"
	label var cov_lexports_0407      	"Log Coefficient of Variation of Exports, mean by sector, 2004-07"
	label var cov_lexpxl_0407 			"Log Coefficient of Variation of Exports per Worker, mean by sector, 2004-07"
	label var numfirms_0407 			"Number of firms in survey, by sector, 2004-07"
	label var delast_S18_usachn 		"Demand Elasticities Soderbery (2017), USA-China"
	label var delast_S18_usa 			"Demand Elasticities Soderbery (2017), median USA"
	label var delast_S18_all 			"Demand Elasticities Soderbery (2017), median all"
	compress
	save "${dir_int}inputs_calibration_sectors.dta", replace
	}
timer off 1
timer list